/**
 *Copyright(c) HNA SYSTEMS Co., LTD
 *
 *@date 2013-7-23
 * 	
 *Original Author: 郑兴(zhengxing)
 *
 *ChangeLog
 *
 */
package com.hnas.sys.service.system.dto;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Collection;
import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import com.hnas.core.util.DateUtil;
import com.hnas.sys.domain.ResourceStatus;


/**
 * @author 郑兴(zhengxing)
 * 
 */
public class DTOUser implements UserDetails{
	/**
	 * 
	 */
	private static final long serialVersionUID = 6123316511690875074L;
	private Long id;
	private String account;
	private String password;
	private String name;
	private ResourceStatus status;
	private int isSystemAdmin;
	private String userNo;
//	private String userName;
//	private DTODepartment dtoCompany;
//	private DTODepartment dtoDepartment;
	private String departmentName;
//	private String postName;
//	private String levelName;
//	private String managementLevel;
//	private String workTypeName;
//	private String workTypeId;
//	private boolean management;
//	private int inpost;
//	private int formal;
	private String email;
//	private String cardNo;
	private String telephone;
//	private boolean retirement;
	private String updateTime;
	private String userRole; // 用户数据权限
	private Date lastVisit;
//	private boolean needLogin;
//	private String menu;
//	private String authorityIDs;
//	private String enterGroupTime;
//	private int showOrder;//显示顺序，默认为0
	private List<DTOAuthority> dtoAuthorities = new ArrayList<DTOAuthority>() ;
	
	


	/*
	 * (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getAuthorities()
	 */
	public Collection<? extends GrantedAuthority> getAuthorities() {
		if(CollectionUtils.isNotEmpty(this.dtoAuthorities)) {
			List<DTOAuthority> activeAuthorities = new ArrayList<DTOAuthority>();
			for(DTOAuthority authority : dtoAuthorities) {
				if(authority.getStatus() != ResourceStatus.INVALID) {
					activeAuthorities.add(authority);
				}
			}
			return activeAuthorities;
		}
		return new ArrayList<DTOAuthority>();
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#getUsername()
	 */
	public String getUsername() {
		return this.account;
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonExpired()
	 */
	public boolean isAccountNonExpired() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isAccountNonLocked()
	 */
	public boolean isAccountNonLocked() {
		return true;
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isCredentialsNonExpired()
	 */
	public boolean isCredentialsNonExpired() {
		return true;
	}

	/**
	 * @return the dtoAuthorities
	 */
	public List<DTOAuthority> getDtoAuthorities() {
		return dtoAuthorities;
	}

	/**
	 * @param dtoAuthorities the dtoAuthorities to set
	 */
	public void setDtoAuthorities(List<DTOAuthority> dtoAuthorities) {
		this.dtoAuthorities = dtoAuthorities;
	}

	/*
	 * (non-Javadoc)
	 * @see org.springframework.security.core.userdetails.UserDetails#isEnabled()
	 */
	public boolean isEnabled() {
		return this.status == ResourceStatus.VALID;
	}

	public Long getId() {
		return id;
	}

	public void setId(Long id) {
		this.id = id;
	}

	public String getAccount() {
		return account;
	}

	public void setAccount(String account) {
		this.account = account;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public ResourceStatus getStatus() {
		return status;
	}

	public void setStatus(ResourceStatus status) {
		this.status = status;
	}

	public int getIsSystemAdmin() {
		return isSystemAdmin;
	}

	public void setIsSystemAdmin(int isSystemAdmin) {
		this.isSystemAdmin = isSystemAdmin;
	}

	public String getUserNo() {
		return userNo;
	}

	public void setUserNo(String userNo) {
		this.userNo = userNo;
	}


	public String getDepartmentName() {
		return departmentName;
	}

	public void setDepartmentName(String departmentName) {
		this.departmentName = departmentName;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getUpdateTime() {
		return updateTime;
	}

	public void setUpdateTime(String updateTime) {
		this.updateTime = updateTime;
	}


	public boolean needLogin() {
		if (null==lastVisit){
			return true;
		}else if (DateUtil.AddDate(new Date(), Calendar.MINUTE, -30).after(lastVisit)){
			return true;
		}
		this.lastVisit = new Date();
		return false;
	}



	public Date getLastVisit() {
		return lastVisit;
	}

	public void setLastVisit(Date lastVisit) {
		this.lastVisit = lastVisit;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	/**
	 * @return the menu
	 */
//	public String getMenu() {
//		HttpSession session = ((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest().getSession(true);
//		Object preURL = session.getAttribute("preURL");
//		Map<DTOMenuPackage, List<DTOResource>> menuMap = new HashMap<DTOMenuPackage, List<DTOResource>>();
//		Map<Long, DTOMenuPackage> menuPackageMap = CacheUtil.getMenuPackageMapCache();
//		//将有权限访问的资源按菜单包分类
//		for(DTOAuthority authority : dtoAuthorities) {
//			for(DTOAuthorityResource dar : authority.getDtoAuthorityResources()) {
//				DTOResource resource = CacheUtil.getResourcesMapCache().get(dar.getDtoResource().getId());
//				DTOMenuPackage menuPackage = menuPackageMap.get(resource.getMenuPackageID());
//				if(menuMap.containsKey(menuPackage)) {
//					menuMap.get(menuPackage).add(resource);
//				} else {
//					List<DTOResource> resources = new ArrayList<DTOResource>();
//					resources.add(resource);
//					menuMap.put(menuPackage, resources);
//				}
//			}
//		}
//		//对菜单包进行排序
//		List<DTOMenuPackage> dmps = new ArrayList<DTOMenuPackage>();
//		dmps.addAll(menuMap.keySet());
//		Collections.sort(dmps);
//		String contextPath = UserUtils.getServletRequest().getContextPath();
//		
//		//当前访问资源：如果当前资源不页面，则显示上上个页面
//		boolean notfind = true;
//		for(DTOResource resource : CacheUtil.getResourceCache()) {
//			if(preURL.toString().equalsIgnoreCase(resource.getResource())) {
//				notfind = false;
//				if(resource.getType() == ResourceType.METHOD || resource.getStatus() == ResourceStatus.INVALID) {
//					preURL = session.getAttribute("showPage");
//				}
//			}
//		}
//		if(notfind) {
//			preURL = session.getAttribute("showPage");
//		}
//
//		//菜单
//		StringBuffer sb = new StringBuffer();
//		for(DTOMenuPackage dmp : dmps) {
//			boolean preURLMatched = false;
//			if(dmp == null || dmp.getStatus() == PackageStatus.HIDDEN) {
//				continue;
//			}
//			sb.append("<li><font>").append(dmp.getName()).append("</font>");
//			sb.append("<div class=\"a_content\"");
//			
//			List<DTOResource> resources = menuMap.get(dmp);
//			Map<Long, Object> duplicateMenu = new HashMap<Long, Object>();
//			Collections.sort(resources);
//			StringBuffer resourceMenu = new StringBuffer();
//			for(DTOResource resource : resources) {
//				if(resource.getType() != ResourceType.PAGE || duplicateMenu.containsKey(resource.getId())) {
//					continue;
//				}
//				resourceMenu.append("<li><a ");
//				if(preURL!=null && preURL.toString().equals(resource.getResource())) {
//					resourceMenu.append("class='menu_hover' ");
//					preURLMatched = true;
//					session.setAttribute("showPage", preURL);
//				}
//				resourceMenu.append("href=\"").append(contextPath);
//				resourceMenu.append(resource.getResource()).append("\">");
//				resourceMenu.append(resource.getName()).append("</a>").append("</li>");
//				duplicateMenu.put(resource.getId(), null);
//			}
//			if(preURLMatched) {
//				sb.append(" style='display:block;'");
//			}
//			sb.append(">").append("<ul>");
//			sb.append(resourceMenu);
//			sb.append("</ul>").append("</div>").append("</li>");
//		}
//		this.menu = sb.toString();
//		return menu;
//	}

	public boolean isValid(){
		return ResourceStatus.VALID.equals(this.status);
	}

	public String getUserRole() {
		return userRole;
	}

	public void setUserRole(String userRole) {
		this.userRole = userRole;
	}

	
	
}
